package cn.stylefeng.guns.modular.util;

import cn.hutool.core.util.StrUtil;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author 刘耀
 * @Date 2022/11/17 16:37
 */
public class PddUtil {

    public static List<ProductDTO> getList(String path) throws Exception {

        List<String> list = readStrList(path);

        //记录有用的信息
        List<ProductDTO> useList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            String x = list.get(i);
            if (x.contains("订单编号：")) {
                ProductDTO dto = new ProductDTO();
                dto.setOrderNo(x.replace("订单编号：", ""));
                dto.setGoodsName(list.get(i + 3));
                dto.setSpecName(list.get(i + 5));
                dto.setGoodsNum(list.get(i + 6).split("\t")[1]);
                dto.setUsername(list.get(i + 8) + list.get(i + 9));
                dto.setMobile(list.get(i + 11));
                dto.setAddress(list.get(i + 13));

                useList.add(dto);
            }
        }

        return useList;
    }


    private static String readFile(String filePath) throws Exception {

        StringBuffer stringBuffer = new StringBuffer();
        Path path = Paths.get(filePath);
        BufferedReader reader = null;
        try {
            reader = Files.newBufferedReader(path, StandardCharsets.UTF_8);

            int ch = 0;

            while ((ch = reader.read()) != -1) {

                stringBuffer.append((char) ch + reader.readLine());

            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            reader.close();
        }
        return stringBuffer.toString();
    }


    private static List<String> readStrList(String filePath) throws Exception {

        List<String> stringList = new ArrayList<>();
        Path path = Paths.get(filePath);
        BufferedReader reader = null;
        try {
            reader = Files.newBufferedReader(path, StandardCharsets.UTF_8);

            int ch = 0;

            while ((ch = reader.read()) != -1) {

                String str = (char) ch + reader.readLine();
                if (StrUtil.isNotBlank(str.trim())) {
                    stringList.add(str.trim());
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            reader.close();
        }
        return stringList;
    }


}
